package com.znow.admin.system.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.znow.admin.dao.entity.sys.SysRoleEntity;
import com.znow.admin.dao.entity.sys.SysUserRoleEntity;
import com.znow.admin.dao.mapper.sys.SysRoleMapper;
import com.znow.admin.dao.mapper.sys.SysUserRoleMapper;
import com.znow.admin.system.security.enums.DeleteEnums;
import com.znow.admin.system.security.model.convert.SysRoleConvert;
import com.znow.admin.system.security.model.res.role.SysRolePageRequest;
import com.znow.admin.system.security.model.res.role.SysRoleRequest;
import com.znow.admin.system.security.model.vo.SysRoleVO;
import com.znow.admin.system.security.service.SysRoleService;
import com.znow.common.exception.ParamsException;
import com.znow.common.model.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统角色服务
 *
 * @author Nemo
 * @date 2023/02/02
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageResult<SysRoleVO> getRoleList(SysRolePageRequest request) {
        LambdaQueryWrapper<SysRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getRoleName())) {
            queryWrapper.like(SysRoleEntity::getRoleName, request.getRoleName());
        }
        queryWrapper.eq(SysRoleEntity::getIsDel, DeleteEnums.NOT_DELETE.getCode());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        final List<SysRoleEntity> roleList = sysRoleMapper.selectList(queryWrapper);
        final List<SysRoleVO> sysRoleVOS = SysRoleConvert.INSTANCE.toSysRoleVoList(roleList);

        return new PageResult(roleList, sysRoleVOS);
    }

    @Override
    public SysRoleVO getRoleDetail(SysRolePageRequest request) {
        if (request.getRoleId() == null) {
            throw new ParamsException("角色ID不能为空！");
        }
        LambdaQueryWrapper<SysRoleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleEntity::getRoleId, request.getRoleId());

        final SysRoleEntity roleEntity = sysRoleMapper.selectOne(queryWrapper);
        return SysRoleConvert.INSTANCE.toSysRoleVo(roleEntity);
    }

    @Override
    public List<SysUserRoleEntity> getRoleListByUserId(Long userId) {
        return sysUserRoleMapper.selectByUserId(userId);
    }

    @Override
    public Boolean saveRole(SysRoleRequest request) {
        if (StringUtils.isBlank(request.getRoleName())) {
            throw new ParamsException("角色名称不能为空！");
        }
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
        sysRoleEntity.setRoleName(request.getRoleName());
        sysRoleEntity.setGmtCreate(new Date());
        sysRoleEntity.setGmtModified(new Date());
        sysRoleMapper.insert(sysRoleEntity);
        return true;
    }

    @Override
    public Boolean updateRole(SysRoleRequest request) {
        if (request.getRoleId() == null) {
            throw new ParamsException("角色ID不能为空！");
        }
        if (StringUtils.isBlank(request.getRoleName())) {
            throw new ParamsException("角色名称不能为空！");
        }
        SysRoleEntity updatePo = new SysRoleEntity();
        updatePo.setRoleName(request.getRoleName());
        updatePo.setRoleId(request.getRoleId());
        updatePo.setRemarks(request.getRemarks());
        updatePo.setGmtCreate(new Date());
        updatePo.setGmtModified(new Date());
        sysRoleMapper.updateById(updatePo);
        return true;
    }

    @Override
    public Boolean deleteRole(SysRoleRequest request) {
        if (request.getRoleId() == null) {
            throw new ParamsException("角色ID不能为空！");
        }
        SysRoleEntity updatePo = new SysRoleEntity();
        updatePo.setRoleId(request.getRoleId());
        updatePo.setIsDel(DeleteEnums.DELETE.getCode());
        updatePo.setGmtModified(new Date());
        sysRoleMapper.updateById(updatePo);
        return true;
    }

}
